//Monday, November 16 2009
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class NextNumber
{
public:
	int getNextNumber(int N) {
// break N into bits
		vector<int> bits(32,0);
		for (int i=0; i<32; i++)
			if (N & 1<<i)
				bits[31-i] = 1;

// find the next permutation of this many 0s and 1s
		next_permutation( bits.begin(), bits.end() );

// convert the bits back into a number
		int res = 0;
		for (int i=0; i<32; i++)
			if (bits[i])
				res += 1<<(31-i);
		return res;
	}
};